package org.lanqiao.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.lanqiao.dao.IEleBeforeBillDao;
import org.lanqiao.dao.IEleBeforeCostDao;
import org.lanqiao.dao.IEleMindDao;
import org.lanqiao.dao.IEleOpeartorDao;
import org.lanqiao.dao.IEleUserDao;
import org.lanqiao.myutil.Myutils;
import org.lanqiao.orm.EleBeforeBill;
import org.lanqiao.orm.EleBeforeCost;
import org.lanqiao.orm.EleMind;
import org.lanqiao.orm.EleUser;
import org.lanqiao.service.INetUserFun;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service("netUserService")
public class NetUserFun implements INetUserFun {

	@Resource
	IEleBeforeBillDao billDao;
	@Resource
	IEleBeforeCostDao coastDao;
	@Resource
	IEleUserDao userDao;
	@Resource
	IEleMindDao mindDao;
	@Resource
	IEleOpeartorDao opDao;

	@Override
	public List<EleBeforeBill> userBillList(int userId, String start, String end) {
		EleBeforeBill bill = new EleBeforeBill();
		bill.setUserid(userId);
		bill.setStart(start);
		bill.setEnd(end);
		// 查询账单
		return billDao.selectBill(bill);
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, Exception.class })
	@Override
	public boolean nowPay(EleUser user, String isPay) {
		// 设置标志位
		boolean result2 = false;
		// 网上缴费的处理
		List<EleBeforeCost> allCoastList = coastDao.selectCost(user.getUserid().toString());
		// 计算需要缴纳的电费
		float allCoast = 0;
		for (EleBeforeCost obj : allCoastList) {
			allCoast += obj.getDownmoney();
		}
		float ispay = Myutils.changeToFloat(isPay);
		// 计算结果
		float remainMoney = ispay + user.getUseradmin() - allCoast;

		user.setUseradmin(remainMoney);

		userDao.updateredmin(user);

		EleBeforeBill nowBill = new EleBeforeBill();

		nowBill.setUserid(user.getUserid());

		nowBill.setUpmoney(ispay);

		if (billDao.inserPayBill(nowBill) > 0) {
			result2 = true;
		}
		coastDao.updateCoast(user.getUserid());
		return true;
	}

	@Override
	public EleUser modifyInfo(EleUser user) {
		EleUser userNew = null;
		// 更新用户自己的信息
		if (userDao.modifyInfo(user) > 0) {
			userNew = userDao.selectOne(user.getUserid());
		}
		return userNew;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, Exception.class })
	@Override
	public boolean modifyPsw(EleUser user, String oldPsw, String newPsw) {
		// 密码修改
		boolean result = false;
		String psw = user.getUserpsw();
		if (oldPsw.equals(psw)) {
			user.setUserpsw(newPsw);
			if (userDao.updatePsw(user) > 0) {
				result = true;
			}
		}
		return result;
	}

	@Override
	public List<EleBeforeCost> selectEleCh(Date start, Date end) {
		HashMap<String, Date> map = new HashMap<String, Date>();
		map.put("start", start);
		map.put("end", end);
		// 查询电费情况
		return coastDao.selectUserEleCh(map);
	}

	@Override
	public EleUser helpPersonSelect(String card) {
		// 查询好友的信息
		return userDao.selectFriend(card);
	}

	@Override
	public boolean helpFriendPay(EleUser user, String money) {
		EleBeforeBill bill = new EleBeforeBill();
		bill.setUserid(user.getUserid());
		bill.setUpmoney(Myutils.changeToFloat(money));
		// 记录为朋友缴费的记录
		return billDao.helpFriendPay(bill) > 0 ? true : false;
	}

	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, Exception.class })
	@Override
	public boolean helpFriend(EleUser user, String money) {
		// 设置标志位
		boolean result2 = false;
		// 网上缴费的处理
		List<EleBeforeCost> allCoastList = coastDao.selectCost(user.getUserid().toString());
		// 计算需要缴纳的电费
		float allCoast = 0;
		for (EleBeforeCost obj : allCoastList) {
			allCoast += obj.getDownmoney();
		}
		float ispay = Myutils.changeToFloat(money);
		// 计算结果
		float remainMoney = ispay + user.getUseradmin() - allCoast;

		user.setUseradmin(remainMoney);

		userDao.updateredmin(user);

		EleBeforeBill nowBill = new EleBeforeBill();

		nowBill.setUserid(user.getUserid());

		nowBill.setUpmoney(ispay);

		if (billDao.insertFriend(nowBill) > 0) {
			result2 = true;
		}
		coastDao.updateCoast(user.getUserid());
		return true;
	}

	@Override
	public boolean addSuggest(String suggest) {
		// 存建议
		return mindDao.insertSugest(suggest) > 0 ? true : false;
	}

	@Override
	public boolean addComplain(EleMind mind) {
		// 添加投诉信息
		return mindDao.insertComplain(mind) > 0 ? true : false;
	}

	@Override
	public List selectAllOp() {
		// 查询所有操作员的信息
		return opDao.selectAllOp();
	}
	
}
